package com.example.navigationpage;

import android.view.View;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import java.util.ArrayList;

/**
 * viewpager控件的适配器
 * 设置要加载的页面
 */
public class NavPagerAdapter extends PagerAdapter {

    //存放View的数组
    private ArrayList<View> Views;
    //存放Title的数组
    private ArrayList<String> Titles;


    public NavPagerAdapter(ArrayList<View> Views, ArrayList<String> Titles) {
        this.Views = Views;
        this.Titles = Titles;
    }

    public NavPagerAdapter(ArrayList<View> Views) {
        this.Views = Views;
    }

    //获取PageTitle
    @Override
    public CharSequence getPageTitle(int position) {
        if (Titles == null) {
            return "";
        } else if (Titles != null && Titles.size() == 0) {
            return "";
        }

        return Titles.get(position);
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView(Views.get(position));
    }

    @Override
    public Object instantiateItem(View container, int position) {
        ((ViewPager) container).addView(Views.get(position), 0);
        return Views.get(position);
    }

    @Override
    public int getCount() {
        if (Views != null) {
            return Views.size();
        }
        return 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }

}
